﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace BSPortal.Usercontrol
{
    public partial class Search : System.Web.UI.UserControl
    {
        public int PageSize;
        private int RowCount;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData(0);
            CreatePagingControl();
        }

        protected void LoadData(int skip)
        {
            string key = Request.QueryString["Keyword"];
            if (string.IsNullOrEmpty(key))
                key = string.Empty;
            key = key.Replace("_", " ");
            lbCategoryName.InnerText = string.Format("Tìm kiếm theo từ khoá: {0}", key);
            short CategoryId = Common.GetLine();

            var listProduct = Common.GetClientService().GetListProductByKey(CategoryId, key).FindAll(row => (bool)row.IsPublished == true);
            var data = listProduct.Skip(skip).Take(PageSize);

            productRepeater.DataSource = data;
            productRepeater.DataBind();

            RowCount = listProduct.Count();
        }

        private void CreatePagingControl()
        {
            int pages = (((RowCount % PageSize) > 0) ? (RowCount / PageSize) + 1 : (RowCount / PageSize));
            if (pages < 2)
            {
                plcPaging.Visible = false;
                return;
            }

            for (int i = 0; i < pages; i++)
            {
                LinkButton lnk = new LinkButton();
                lnk.Click += new EventHandler(LblClick);
                lnk.ID = "lnkPage" + (i + 1).ToString();
                lnk.Text = (i + 1).ToString();
                plcPaging.Controls.Add(lnk);
                Label spacer = new Label();
                spacer.Text = "&nbsp;";
                plcPaging.Controls.Add(spacer);
            }
        }

        void LblClick(object sender, EventArgs e)
        {
            LinkButton lnk = sender as LinkButton;
            int currentPage = int.Parse(lnk.Text);
            int skip = (currentPage - 1) * PageSize;
            LoadData(skip);
        }

        protected void productRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if (e.Item is RepeaterItem)//for item template
                {
                    Label lblDiscount = (Label)e.Item.FindControl("lblDiscount");
                    Label txtDiscount = (Label)e.Item.FindControl("txtDiscount");
                    Label lblUnit = (Label)e.Item.FindControl("lblUnit");
                    Label lblPrice_Title = (Label)e.Item.FindControl("lblPrice_Title");

                    Product product = ((Product)e.Item.DataItem);
                    if (product.Discount == null || product.Discount == 0)
                    {
                        lblDiscount.Visible = false;
                        txtDiscount.Visible = false;
                        lblUnit.Visible = false;
                        lblPrice_Title.Text = " Giá bán: ";
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}